<template>
  <div>
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      label-position="top"
      label-width="80px"
    >
      <!-- <el-form-item v-if="rejectUser" label="驳回至">
        <el-tag>{{ rejectUser }}</el-tag>
      </el-form-item> -->
      <el-form-item label="驳回意见" prop="remarks">
        <el-input
          v-model.trim="formData.remarks"
          type="textarea"
          :rows="3"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-row justify="end">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { defineEmits, reactive, ref, defineProps } from "vue";
import { postFlowReject, postComFlowSubmit } from "/@/api/acceptance";
import { useRouter, useRoute } from "vue-router";
import NotifMessage from "/@/utils/message/notification";

const router = useRouter();
const route = useRoute();
const formRef = ref<HTMLFormElement | null>(null) as any;
const emit = defineEmits(["update:dialogVisible"]);
const props = defineProps({
  taskId: {
    type: String,
    default: ""
  },
  // recordId: {
  //   type: Number
  // },
  rejectUser: {
    type: String,
    default: ""
  },
  isChargehandReject: {
    type: String,
    default: ""
  }
});

let formData = reactive({
  remarks: ""
});

const formRules = {
  remarks: {
    required: true,
    message: "请输入驳回意见",
    trigger: "change"
  }
};

const handleSubmit = async () => {
  try {
    // if (!route.query?.nodeDefId) {
    //   return;
    // }
    const isValid = await new Promise(resolve =>
      formRef.value.validate(resolve)
    );
    if (!isValid) return;

    if (props.isChargehandReject) {
      const { nodeDefId, instanceId, contractId, flowId, flowMinType } =
        route.query;
      const params = {
        formBaseInfo: {
          id: contractId
        },
        formSubMap: {
          flowType: flowMinType,
          nodeId: nodeDefId,
          // specialReject: "", // 点击驳回实际调用提交接口，如果驳回值为 1
          isChargehand: "2", // 1 不跳过副组长 0 跳过  2 驳回
          approvalNode: "", //  副组长 101  执行组长102
          procInstId: instanceId,
          taskId: props.taskId,
          opinion: `#e#驳回意见：${formData.remarks}`,
          flowId,
          assignee: props.isChargehandReject // 提交下一流程处理人Id
        }
      };
      const { code } = await postComFlowSubmit(params);
      if (code === 200) {
        NotifMessage.success("驳回成功");
        handleCancel();
        router.replace("/acceptance/concealed");
      }
    } else {
      const { nodeDefId, instanceId } = route.query;
      const params = {
        formRejectMap: {
          nodeId: nodeDefId,
          procInstId: instanceId,
          taskId: props.taskId,
          opinion: `#e#驳回意见：${formData.remarks}`
          // recordId: props.recordId
        }
      };
      const { code } = await postFlowReject(params);
      if (code === 200) {
        NotifMessage.success("驳回成功");
        handleCancel();
        // router.replace("/acceptance/concealed");
        handleBack();
      }
    }
  } catch (err) {
    console.log(err);
  }
};

// 返回按钮事件
const handleBack = () => {
  // TODO  根据来源进行处理
  if (route.query.source === "work") {
    router.push({ path: "/work/dashboard", replace: true });
  } else {
    router.push({ path: "/acceptance/concealed", replace: true });
  }
};

const handleCancel = () => {
  emit("update:dialogVisible", false);
};
</script>
